package com.cskaoyan.mall.controller.systemmanage;

import com.cskaoyan.mall.bean.Admin;
import com.cskaoyan.mall.bean.Log;
import com.cskaoyan.mall.bean.Storage;
import com.cskaoyan.mall.bean.vo.ResultVO;
import com.cskaoyan.mall.properties.FileUploadProperties;
import com.cskaoyan.mall.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * Created by Zsp
 * 2021/1/10
 */
@RestController
@RequestMapping("admin/storage")
public class StorageController {

    @Autowired
    AdminService adminService;
    @Autowired
    FileUploadProperties fileUploadProperties;

    @PostMapping("create")
    public ResultVO create(MultipartFile file, HttpServletRequest request) {
        Log log = new Log(null, (String) request.getSession().getAttribute("username"), request.getRemoteAddr(), 1,
                "存储对象", null, null, null, new Date(), new Date(), false);
        if (file != null || !file.isEmpty()) {
            try {
                String originalFilename = file.getOriginalFilename();
                String pathname = "" + UUID.randomUUID() + new Date().getTime() + originalFilename;
                File destFile = new File(fileUploadProperties.getAdminAvatarPath() + pathname);
                if (!destFile.getParentFile().exists()) {
                    destFile.mkdirs();
                }
                file.transferTo(destFile);
                Storage storage = adminService.createStorage(new Storage(null, originalFilename, originalFilename, file.getContentType(),
                        (int) file.getSize(), fileUploadProperties.getAdminAvatarURL() + pathname, new Date(), new Date(), false));
                if (storage != null) {
                    log.setStatus(true);
                    log.setResult("存储对象成功");
                    adminService.insertLog(log);
                    return ResultVO.ok(storage);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        log.setStatus(false);
        log.setResult("存储对象失败，文件为空");
        adminService.insertLog(log);
        return ResultVO.error(10000, "文件不能为空");
    }

    @RequestMapping("list")
    public ResultVO getAdminList(Integer page, Integer limit, String sort, String order, String name, String key) {
        return ResultVO.ok(adminService.getStorageList(page, limit, sort, order, name, key));
    }

    @PostMapping("update")
    public ResultVO update(@RequestBody Storage storage, HttpServletRequest request) {
        Log log = new Log(null, (String) request.getSession().getAttribute("username"), request.getRemoteAddr(), 1,
                "修改存储对象", null, null, null, new Date(), new Date(), false);
        return adminService.updateStorage(storage, log);
    }

    @PostMapping("delete")
    public ResultVO delete(@RequestBody Storage storage, HttpServletRequest request) {
        Log log = new Log(null, (String) request.getSession().getAttribute("username"), request.getRemoteAddr(), 1,
                "删除存储对象", null, null, null, new Date(), new Date(), false);
        return adminService.deleteStorage(storage, log);
    }


}
